import { changeForm } from '@u/form.js';
import verify from './verify.js';
import getEnums from './enum.js';

export default {
  data() {
    return {
      id: this.$route.params.id,
      formObj: {
        type: null,
        source: '',
        image: 0,
        contents: '',
      },
      opts: {
        title: {
          label: '文章标题：',
          value: '',
          required: true,
          position: [1, 1],
        },
        type_source: {
          slot: true,
          tabs: 2,
          position: [2, 1],
        },
        img: {
          label: '文章头图：',
          slot: true,
          required: true,
          position: [2, 2],
        },
        contents: {
          label: '文章内容：',
          slot: true,
          required: true,
          position: [3, 1],
        },
      },
      tipContent: '请上传比例比例3:1，小于等于2M的图片\n支持扩展名：.jpg .jpeg .png',
      typeOptions: [],
      cancelMessage: {
        msg: '确定取消吗？',
        title: '取消确认',
        options: {}
      },
      cancelNavTo: { path: '' },
      confirmNavTo: { path: '' }
    };
  },
  async created() {
    if (this.id) {
      await this.getDetail();
    }
    getEnums(this);
  },
  activated() {
    this.$store.commit('layout/initManuallyRefresh', async() => {
      getEnums(this);
    });
  },
  methods: {
    async getDetail() {
      const res = {
        code: 200,
        data: {
          title: '标题标题标题标题',
          typeObj: {
            id: 2,
            name: '通知公告',
          },
          source: '来源来源来源',
          imageObj: {
            id: 267,
            path: 'http://192.168.1.136:4010//UploadFile/File/6c7766d2-1207-4b10-97c2-1945921f0500.png',
          },
          contents: '<p>内容内容</p>',
        }
      };

      // const res = await this.detailFn(this.id);
      if (res.code == 200) {
        this.formObj.type = res.data.typeObj.id;
        this.formObj.source = res.data.source;
        this.formObj.image = res.data.imageObj.id;
        this.formObj.contents = res.data.contents;
        res.data.imageObj.url = res.data.imageObj.path;
        this.fileObj = res.data.imageObj;
        changeForm(this.opts, res.data, true);
      }
    },
    async confirm() {
      const params = changeForm(this.opts, this.formObj);
      if (verify(this, params)) {
        let res = {};
        if (!this.id) {
          res = await this.addFn({ data: params });
          this.confirmNavTo.params.id = res.data.id;
        } else {
          res = await this.editFn(this.id, { data: params });
        }
        if (res.code == 200) {
          this.confirmNavTo.params.pages = { Permission: this.chains };
          this.$router.push(this.confirmNavTo);
        }
      }
    },
    cancel() {
      this.$confirm(this.cancelMessage.msg, this.cancelMessage.title, {
        type: 'warning',
        ...this.cancelMessage.options
      }).then(() => {
        this.navigateBack(this.cancelNavTo);
      }).catch(() => {});
    }
  },
  components: {
    Form: () => import('@c/Form.vue'),
    Editor: () => import('@c/Editor'),
    PhotoUpload: () => import('@c/PhotoUpload'),
  }
};
